package com.fc.advbox.controller.api;

import com.fc.advbox.common.ResponseJson;
import com.fc.advbox.controller.admin.BaseController;
import com.fc.advbox.dto.org.ContractRequest;
import com.fc.advbox.entity.ContractEntity;
import com.fc.advbox.entity.CustomerContractEntity;
import com.fc.advbox.service.ICustomerContractService;
import com.fc.advbox.utils.PageUtils;
import com.fc.advbox.utils.Query;
import com.fc.advbox.utils.validator.ValidatorUtils;
import com.fc.advbox.utils.validator.group.AddGroup;
import com.fc.advbox.utils.validator.group.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * ContractController
 *
 * @author Gary.Jiang
 * @date 2018/6/22.15:37
 */
@RestController
@RequestMapping("/api/contract/customer")
@Api(value="合同接口", tags={"合同接口"})
@Slf4j
public class CustomerContractController extends BaseController {
    @Autowired
    private ICustomerContractService customerContractService;

    /**
     * 投资合同列表
     */
    @PostMapping("/list")
    @ApiOperation(value = "查询客户合同列表", httpMethod="POST",
            notes = " REST 方式请求," +
                    "Request Params: <ul>" +
                    "<li><i>查询合同到期提醒列表请添加参数: interva, 如查询30天到期, interva=30" +
                    "<li><i>可选参数: name: string </i></li>" +
                    "<li><i>可选参数: type: string </i></li>" +
                    "<li><i>可选参数: area: string </i></li>" +
                    "<li><i>可选参数: city: string </i></li>" +
                    "<li><i>可选参数: status: int (0,1) </i></li>" +
                    "<li><i>可选参数: startDate: 开始时间  格式：yyyy-MM-dd HH:mm:SS 例如 2018-06-19 07:54:01</i></li>" +
                    "<li><i>可选参数: endDate: 结束时间  格式：yyyy-MM-dd HH:mm:SS 例如 2018-06-19 07:54:01</i></li>" +
                    "<li><i>默认参数: page: 1, 当前页，默认值1 </i></li>" +
                    "<li><i>默认参数: limit: 20 </i></li>" +
                    "</ul>")
    public ResponseJson<PageUtils<CustomerContractEntity>> query(@RequestBody(required = false) ContractRequest request){
        if(request == null){
            request = new ContractRequest();
        }

        Map<String, Object> params = request.toMap();
        log.info("query customer contracts, params:{}", request);
        //校对并赋值 offset和limit
        calculatePagination(params);

        Query query = new Query(params);
        List<CustomerContractEntity> list = customerContractService.queryList(query);
        int total = customerContractService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());

        return ResponseJson.success(pageUtil);
    }

    /**
     * 获取单个合同
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "获取单个客户合同", httpMethod="GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", required = true, dataType = "long", value="合同ID"),
    })
    public ResponseJson<CustomerContractEntity> get(@PathVariable Long id){
        CustomerContractEntity contractEntity = customerContractService.queryObject(id);
        return ResponseJson.success(contractEntity);
    }

    /**
     * 保存合同
     */
    @PostMapping("/save")
    @ApiOperation(value = "添加客户合同", response = ResponseJson.class, httpMethod="POST",
            notes = " REST 方式请求" +
                    "Request Body: <ul>" +
                    "<li><i>userId: 客户Id </i></li>" +
                    "<li><i>name: 合同名称 </i></li>" +
                    "<li><i>province: 省份 </i></li>" +
                    "<li><i>city: 城市 </i></li>" +
                    "<li><i>area: 地区 </i></li>" +
                    "<li><i>contactor: 联系人 </i></li>" +
                    "<li><i>contactPhone: 联系人电话 </i></li>" +
                    "<li><i>descn: 描述 </i></li>" +
                    "<li><i>type: 类型 </i></li>" +
                    "<li><i>amount: 金额 </i></li>" +
                    "<li><i>equipmentNum: 设备数量 </i></li>" +
                    "<li><i>startDate: 开始时间  格式：yyyy-MM-dd HH:mm:SS 例如 2018-06-19 07:54:01</i></li>" +
                    "<li><i>endDate: 结束时间  格式：yyyy-MM-dd HH:mm:SS 例如 2018-06-19 07:54:01</i></li>" +
                    "</ul>")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "entity", required = true, dataType = "CustomerContractEntity", value="合同对象"),
    })
    public ResponseJson save(@RequestBody CustomerContractEntity entity){
        ValidatorUtils.validateEntity(entity, AddGroup.class);

        customerContractService.save(entity);

        return ResponseJson.success();
    }

    /**
     * 更新合同
     */
    @PutMapping("/update")
    @ApiOperation(value = "更新客户合同", response = ResponseJson.class, httpMethod="PUT",
            notes = " REST 方式请求" +
                    "Request Body: <ul>" +
                    "<li><i>userId: 客户Id </i></li>" +
                    "<li><i>name: 合同名称 </i></li>" +
                    "<li><i>province: 省份 </i></li>" +
                    "<li><i>city: 城市 </i></li>" +
                    "<li><i>area: 地区 </i></li>" +
                    "<li><i>contactor: 联系人 </i></li>" +
                    "<li><i>contactPhone: 联系人电话 </i></li>" +
                    "<li><i>descn: 描述 </i></li>" +
                    "<li><i>type: 类型 </i></li>" +
                    "<li><i>amount: 金额 </i></li>" +
                    "<li><i>equipmentNum: 设备数量 </i></li>" +
                    "<li><i>startDate: 开始时间  格式：yyyy-MM-dd HH:mm:SS 例如 2018-06-19 07:54:01</i></li>" +
                    "<li><i>endDate: 结束时间  格式：yyyy-MM-dd HH:mm:SS 例如 2018-06-19 07:54:01</i></li>" +
                    "</ul>")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "entity", required = true, dataType = "CustomerContractEntity", value="合同对象"),
    })
    public ResponseJson update(@RequestBody CustomerContractEntity entity){
        ValidatorUtils.validateEntity(entity, UpdateGroup.class);

        customerContractService.update(entity);

        return ResponseJson.success();
    }

    /**
     * 删除合同
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除客户合同", response = ResponseJson.class, httpMethod="DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", required = true, dataType = "long", value="合同ID"),
    })
    public ResponseJson delete(@PathVariable Long id){
        customerContractService.delete(id);
        return ResponseJson.success();
    }

}
